{$CLEO .cs}
{$USE cleo+}
0000:
     27@ = audiostream.Load3D("cleo\sounds\Damage2.ogg")  
     
int sentry_gun
int shooter
int victim  
Model.Load(#MINIGUN)
Model.Load(1271)
Model.Load(2985)
Model.Load(#ARMY)
Model.Load(#HEATSEEK)
load_requested_models
31@=0 

:REPLACE
:SENTRY
wait 0 
if and
   coop
   Model.Available(2985)
   Model.Available(1271)
   Model.Available(#ARMY)
//   Model.Available(#MINIGUN)
jf @SENTRY
04C4: store_coords_to 7@ 8@ 9@ from_actor player2 with_offset 0.0 1.0 -1.0 
sentry_gun = Object.Init(2985,  7@, 8@, 9@) 



  load_animation "BOMBER"   

 0382: set_object sentry_gun collision_detection 0
 6@=actor.Angle(player2)
 object.Angle(sentry_gun)=6@
 069B: attach_object sentry_gun to_actor player2 with_offset 0.0 0.6 -1.0 rotation 0.0 0.0 90.0 
 06AB: set_actor player2 all_weapons_hidden 1
 audiostream.LinkToObject(27@  ,sentry_gun)
 while true 
 wait 0
    if and 
    is_object_defined sentry_gun 
    00E1:   player 1 pressed_key 17
    coop
    then
        break 
    end
    if not coop
    then
       if is_object_defined sentry_gun 
       then
          object.Destroy( sentry_gun )

       end
            Model.Destroy(#MINIGUN)
            Model.Destroy(1271)
            Model.Destroy(2985)
            Model.Destroy(#ARMY)
            Model.Destroy(#HEATSEEK) 
               release_animation "BOMBER"  
               release_animation "FIGHT_D"    
            audiostream.Release(27@ )
            terminate_this_custom_script          
                 
       
    end 
 end  
 06AB: set_actor player2 all_weapons_hidden 0

 :1
 wait 0
 if and
 coop
 Model.Available(#ARMY)
 jf @1
 0812: AS_actor player2  perform_animation "BOM_Plant_2Idle" IFP "BOMBER" framedelta 4.0 loopA 0 lockX 0 lockY 0 lockF 0 time -1  // versionB

 0682: detach_object  sentry_gun 0.0 0.0 0.0 collision_detection 0
0400: store_coords_to 7@ 8@ 9@ from_object sentry_gun with_offset 0.0 0.0 0.0
 shooter=actor.Create(8,#ARMY, 7@, 8@, 9@)
 actor.Health(shooter)=3000 
 0ED9: set_char_ignore_damage_anims shooter 1 
0946: set_actor shooter actions_uninterupted_by_weapon_fire 1 
060B: set_actor shooter decision_maker_to 32 
0489: set_actor shooter muted 1 
0446: set_actor shooter dismemberment_possible 0
actor.WeaponAccuracy( shooter ) = 100
07DD: set_actor shooter attack_rate 100 // previously known as temper_to
actor.GiveWeaponAndAmmo( shooter , Minigun , 9999999)
087E: set_actor shooter weapon_droppable 0
0337: set_actor shooter visibility 0
 0619: enable_actor shooter collision_detection 1
0638: AS_actor shooter stay_put 1
081A: set_actor shooter weapon_skill_to 2

actor.SetImmunities( shooter, 0,1,0,0,0)
0382: set_object sentry_gun collision_detection 1

069B: attach_object sentry_gun to_actor shooter with_offset 0.0 0.0 -1.0 rotation 0.0 0.0 90.0


10@=0
32@=0
33@=50000

30@=actor.Angle(player2  )
actor.Angle( shooter ) = 30@
:to
while true
actor.GiveWeaponAndAmmo( shooter , Minigun , 9999999)

wait 0
if   and
  not is_char_playing_any_script_animation shooter include_anims 1
 32@ > 3000
then
     
    31@ += 1
    32@ = 0 
    if 10@ == 0 
    then
        10@ = 1  
        jump @TO 
    end
    if 10@ == 1 
    then
        10@ = 0 
        jump @TO 
    end
   
    
end

if 82E0: not  actor shooter firing_weapon
then

actor.Angle( shooter ) = 30@
end
if 10@ == 0 
then
   034D: rotate_object sentry_gun from_angle 0.0 to_angle 360.0 flag 0
    30@ += 2.0
end
if 10@ == 1 
then
   034D: rotate_object sentry_gun from_angle 360.0 to_angle 0.0 flag 0
    30@ -= 2.0
end

0AE1: victim = random_char_near_point 0.0 0.0 0.0 in_radius 9999999999.0 find_next 1 pass_deads 1
    if is_actor_defined victim
    then
        089F: get_actor victim pedtype_to 19@

         if  not 19@==8
         then
             if and
             not actor.Dead( shooter )
             33@ > 500
             0364:   actor shooter  spotted_actor victim
             0474:   actor victim near_object_in_cube sentry_gun radius 120.0 120.0 120.0 sphere 0
               not is_char_playing_any_script_animation shooter include_anims 1

             then
           //  04C4: store_coords_to 7@ 8@ 9@ from_actor victim with_offset 0.0 0.0 0.0 
          //   05A2: set_object sentry_gun rotation_velocity_about_an_axis 7@ 8@ 9@   through_center_of_mass
           //  05A1: set_object sentry_gun rotation_velocity_about_an_axis 7@ 8@ 9@   through_center_of_body

            // 0668: AS_actor shooter rotate_and_shoot_at 7@ 8@ 9@  8000 ms
          //   05E2: AS_actor shooter kill_actor victim
              07A5: AS_actor shooter attack_actor victim time 8000

             33@=0
             end
             if actor.Dead(victim)
             then
                33@=900000
             end
         end
    end
    if or
    
    0366:   object sentry_gun damaged 
    actor.Dead( shooter)
    then
        
       break
    end
    if or
       $onmission == 0 
     31@ >= 20 
    then
       jump   @KILL_TURRENT 
    end
    if coop
    then
        if  
        0474:   actor player1 near_object_in_cube sentry_gun radius 3.0 3.0 3.0 sphere 0
        then
            if  and 
               not is_char_playing_any_script_animation player1 include_anims 1
               0474:   actor player1 near_object_in_cube sentry_gun radius 3.0 3.0 3.0 sphere 0
               00E1:   player 0 pressed_key 11 
            then

                 0812: AS_actor player1  perform_animation "FightD_1" IFP "FIGHT_D" framedelta 4.0 loopA 0 lockX 1 lockY 1 lockF 0 time -1  // versionB
                  wait 300
                21@=actor.Angle(player1)
                21@+=180.0
                object.Angle(sentry_gun)=21@
                  
                  audiostream.PerformAction( 27@,play)
                 jump @KILL_BY_OTHERS
            end
    
        end
        if  
        0474:   actor player2 near_object_in_cube sentry_gun radius 3.0 3.0 3.0 sphere 0
        then
           // show_text_highpriority "Press ~y~~k~~CONVERSATION_YES~~w~ to turn off Sentry Gun." 1
            if   and
               not is_char_playing_any_script_animation player2 include_anims 1
               00E1:   player 1 pressed_key 11 
            then
                21@=actor.Angle(player2)
                21@+=180.0
                object.Angle(sentry_gun)=21@

                 0812: AS_actor player2  perform_animation "FightD_1" IFP "FIGHT_D" framedelta 4.0 loopA 0 lockX 1 lockY 1 lockF 0 time -1  // versionB
                  wait 300
                  
                  audiostream.PerformAction( 27@,play)
                 jump @TURN_OFF 
            end
            if     00E1:   player 1 pressed_key 10 
            then
                 actor.DestroyInstantly( shooter )
                 object.Destroy(sentry_gun )
                 jump @REPLACE
            end
        end
    end
end    
0453: set_object sentry_gun XY_rotation 90.0 0.0 angle -90.0 
21@=object.Angle(sentry_gun)
21@+=90.0
0382: set_object sentry_gun collision_detection 0
0400: store_coords_to 7@ 8@ 9@ from_object sentry_gun with_offset 0.0 0.0 0.0
 0948: create_explosion_at 7@ 8@ 9@ type 3 camera_shake 1.0
   release_animation "BOMBER"   
   release_animation "FIGHT_D"   
  actor.DestroyInstantly( shooter )
  object.RemoveReferences(sentry_gun ) 
 audiostream.Release(27@ )
 gosub @Clean

  terminate_this_custom_script
:TURN_OFF 

0453: set_object sentry_gun XY_rotation 90.0 0.0 angle 0.0
0382: set_object sentry_gun collision_detection 0
 
 
   release_animation "BOMBER"  
   release_animation "FIGHT_D"   
  actor.DestroyInstantly( shooter )
  object.RemoveReferences(sentry_gun )
// 09A2: destroy_object_with_fade sentry_gun
wait 4000
  audiostream.Release(27@ )
gosub @Clean
terminate_this_custom_script

:KILL_TURRENT  
0382: set_object sentry_gun collision_detection 0
   release_animation "BOMBER"  
   release_animation "FIGHT_D"   
  actor.DestroyInstantly( shooter )
  object.Destroy(sentry_gun )
   audiostream.Release(27@ )
gosub @Clean
terminate_this_custom_script

:KILL_BY_OTHERS 

0453: set_object sentry_gun XY_rotation 90.0 0.0 angle 21@
0382: set_object sentry_gun collision_detection 0
 
   release_animation "BOMBER"  
   release_animation "FIGHT_D"   
  actor.DestroyInstantly( shooter )
  object.RemoveReferences(sentry_gun )
// 09A2: destroy_object_with_fade sentry_gun
wait 4000
gosub @Clean
  audiostream.Release(27@ )
terminate_this_custom_script

:Clean
Model.Destroy(#MINIGUN)
Model.Destroy(1271)
Model.Destroy(2985)
Model.Destroy(#ARMY)
Model.Destroy(#HEATSEEK)

return